package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.LeaveStudent;
import cn.wolfcode.crm.domain.Officialstu;
import cn.wolfcode.crm.mapper.LeaveStudentMapper;
import cn.wolfcode.crm.mapper.OfficialstuMapper;
import cn.wolfcode.crm.query.QueryForObject;
import cn.wolfcode.crm.service.ILeaveStudentService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LeaveStudentServiceImpl implements ILeaveStudentService {
    @Autowired
    private LeaveStudentMapper mapper;
    @Autowired
    private OfficialstuMapper officialstuMapper;

    public void deleteByPrimaryKey(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void insert(LeaveStudent record) {
        mapper.insert(record);

    }

    public LeaveStudent selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<LeaveStudent> selectAll() {
        return mapper.selectAll();
    }

    public void updateByPrimaryKey(LeaveStudent record) {
        mapper.updateByPrimaryKey(record);
    }

    public PageResult query(QueryForObject qo) {
        int total = mapper.queryForTotal(qo);
        if (total == 0) {
            return new PageResult();
        }

        List<LeaveStudent> rows = mapper.queryForList(qo);
        return new PageResult(total, rows);
    }

    //流失学员记录保存入库
    @Override
    public void save(LeaveStudent leaveStudent) {
        //先根据传上来要流失的学员id查到学员信息
        Officialstu officialstu = officialstuMapper.selectByPrimaryKey(leaveStudent.getOfficialstu().getId());

        leaveStudent.setLeaveClass(officialstu.getIttclass());//流失班级
        leaveStudent.setOperator((Employee) SecurityUtils.getSubject().getPrincipal());//经办人--获取当前登入用户
        leaveStudent.setInputUser((Employee) SecurityUtils.getSubject().getPrincipal());//录入人--获取当前登入用户
        leaveStudent.setSaleman(officialstu.getMarketer());//营销人员
        leaveStudent.setAuditState(0);//审核状态
        leaveStudent.setQq(officialstu.getQqNum());//qq
        leaveStudent.setTel(officialstu.getTel());//电话

        mapper.insert(leaveStudent);//保存流失学员信息
        officialstuMapper.changeState(officialstu.getId(),4);

    }

    @Override
    public LeaveStudent queryByIdForNameAndRefundAmount(Long stuId) {
        return mapper.queryByIdForNameAndRefundAmount(stuId);
    }

    @Override
    public void changeState(Long id) {
        mapper.changeState(id);
    }
}
